<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="80px"
    >
      <el-form-item label="商品名称" prop="commodityName">
        <el-input
          v-model="queryParams.commodityName"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="老师姓名" prop="teacherName">
        <el-input
          v-model="queryParams.teacherName"
          placeholder="请输入老师姓名"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
<!--      <el-form-item label="场地" prop="venueId">-->
<!--        <el-select-->
<!--          v-model="queryParams.venueId"-->
<!--          placeholder="请选择场地"-->
<!--          clearable-->
<!--          filterable-->
<!--          @keyup.enter="handleQuery"-->
<!--          class="!w-240px"-->
<!--        >-->
<!--          <el-option-->
<!--            v-for="item in venueList"-->
<!--            :key="item.id"-->
<!--            :label="item.venueNo"-->
<!--            :value="item.id"-->
<!--          />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          @click="openForm('create')"-->
<!--          v-hasPermi="['train:vip-classes:create']"-->
<!--        >-->
<!--          <Icon icon="ep:plus" class="mr-5px" /> 新增-->
<!--        </el-button>-->
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['train:vip-classes:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          :disabled="isEmpty(checkedIds)"-->
<!--          @click="handleDeleteBatch"-->
<!--          v-hasPermi="['train:vip-classes:delete']"-->
<!--        >-->
<!--          <Icon icon="ep:delete" class="mr-5px" /> 批量删除-->
<!--        </el-button>-->
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table
      row-key="id"
      v-loading="loading"
      :data="list"
      :stripe="true"
      :show-overflow-tooltip="true"
      @selection-change="handleRowCheckboxChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column align="center" label="序号" width="55">
        <template #default="scope">
          {{ (queryParams.pageNo - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="商品名称" align="center" prop="commodityName" min-width="180" />
      <el-table-column label="老师姓名" align="center" prop="teacherName" />
      <el-table-column label="学生人数" align="center" prop="studentQuantity" />
      <el-table-column label="操作" align="center" min-width="180px">
        <template #default="scope">
          <div class="flex items-center justify-center">
<!--            <el-button-->
<!--              link-->
<!--              type="primary"-->
<!--              @click="openForm('update', scope.row.id)"-->
<!--              v-hasPermi="['train:vip-classes:update']"-->
<!--            >-->
<!--              编辑-->
<!--            </el-button>-->
<!--            <el-button-->
<!--              link-->
<!--              type="danger"-->
<!--              @click="handleDelete(scope.row.id)"-->
<!--              v-hasPermi="['train:vip-classes:delete']"-->
<!--            >-->
<!--              删除-->
<!--            </el-button>-->
<!--            <el-divider direction="vertical" />-->
            <!-- 管理操作 -->
            <el-dropdown v-hasPermi="['train:vip-classes:update']" @command="handleCommand($event, scope.row)" trigger="hover">
              <el-button link type="primary">
                更多操作
                <Icon icon="ep:arrow-down" class="ml-3px" />
              </el-button>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item command="students"><Icon icon="ep:user"/>课程学生列表</el-dropdown-item>
                  <el-dropdown-item command="attendance"><Icon icon="ep:calendar"/>课程签到记录</el-dropdown-item>
                  <el-dropdown-item command="work"><Icon icon="ep:document"/>课程作品管理</el-dropdown-item>
                  <el-dropdown-item command="grades"><Icon icon="ep:trophy"/>课程成绩管理</el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <VipClassesForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { isEmpty } from '@/utils/is'
const { push } = useRouter()
import download from '@/utils/download'
import { VipClassesApi, VipClasses } from '@/api/train/vipclasses'
import VipClassesForm from './VipClassesForm.vue'
import { VenueApi } from '@/api/train/venue'

/** 班级 列表 */
defineOptions({ name: 'CourseClasses' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<VipClasses[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  classNumber: undefined,
  commodityId: undefined,
  studentQuantity: undefined,
  courseName: undefined,
  commodityName: undefined,
  teacherName: undefined,
  venueNo: undefined,
  venueId: undefined,
  allCourseTime: [],
  remainderCourseTime: [],
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中
const venueList = ref([])

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await VipClassesApi.getVipClassesPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

const getVenueList = async () => {
  loading.value = true
  try {
    venueList.value = await VenueApi.getVenueList()
  } catch (e) {}
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await VipClassesApi.deleteVipClasses(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 批量删除班级 */
const handleDeleteBatch = async () => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    await VipClassesApi.deleteVipClassesList(checkedIds.value);
    message.success(t('common.delSuccess'))
    await getList();
  } catch {}
}

const checkedIds = ref<number[]>([])
const handleRowCheckboxChange = (records: VipClasses[]) => {
  checkedIds.value = records.map((item) => item.id);
}

// 处理更多操作
function handleCommand(command, row) {

  switch (command) {
    case 'students':
      // 跳转到课程学生列表
      push('/teaching/course-student-regist-form/vip/' + row.id + '?vip=true')
      break;
    case 'attendance':
      // 跳转到课程签到记录
      push('/teaching/scan-record/vip/' + row.id + '?vip=true')
      break;
    case 'work':
      // 跳转到课程作品管理
      push('/teaching/student-course-performance/vip/' + row.id + '?vip=true')
      break;
    case 'grades':
      // 跳转到课程成绩管理
      push('/teaching/student-course-score/vip/' + row.id + '?vip=true')
      break;
  }
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await VipClassesApi.exportVipClasses(queryParams)
    download.excel(data, '班级.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
  getVenueList()
})
</script>

<style lang="scss" scoped>
.el-dropdown-link {
  cursor: pointer;
  color: #409EFF;
  display: flex;
  align-items: center;
  outline: none !important;
}

:deep(.el-tooltip__trigger:focus-visible) {
  outline: unset;
}
</style>
